<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">

		<u-form :model="info" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">

			<u-form-item v-if="judgeShow('tm001')" label="单别">
				<u-input v-model="buyName" placeholder="单别" disabled></u-input>
			</u-form-item>
			<u-form-item label="单号" v-if="judgeShow('tm002')">
				<u-input v-model="info.tm002" placeholder="单号" :disabled="judgeWrite('tm002')"></u-input>
			</u-form-item>
			<u-form-item label="单据日期" v-if="judgeShow('tm003')">
				<u-input v-model="info.tm003" placeholder="单据日期" :disabled="judgeWrite('tm010')"></u-input>
			</u-form-item>

			<u-form-item label="委外供应商" labelWidth='160' v-if="judgeShow('tm004')">
				<u-input v-model="buyTwoName" placeholder="委外供应商" disabled></u-input>
			</u-form-item>

			<u-form-item label="币种" v-if="judgeShow('tm005')">
				<u-input v-model="payTypeName" placeholder="币种" disabled></u-input>
			</u-form-item>
			<u-form-item label="付款条件" v-if="judgeShow('ma025')">
				<u-input v-model="info.ma025" placeholder="付款条件" :disabled="judgeWrite('ma025')"></u-input>
			</u-form-item>
			<u-form-item label="价格说明" v-if="judgeShow('ma026')">
				<u-input v-model="info.ma026" placeholder="价格说明" :disabled="judgeWrite('ma026')"></u-input>
			</u-form-item>
			<u-form-item label="审核者" v-if="judgeShow('tm011')">
				<u-input v-model="info.tm011" placeholder="审核者" :disabled="judgeWrite('tm011')"></u-input>
			</u-form-item>
			<u-form-item label="核价日期" v-if="judgeShow('tm003')">
				<u-input v-model="info.tm003" placeholder="核价日期" disabled></u-input>
			</u-form-item>
			<u-form-item label="签核状态" v-if="judgeShow('tm012')">
				<u-input v-model="statusName" placeholder="签核状态" disabled></u-input>
			</u-form-item>

			<u-form-item label="是否含税" v-if="judgeShow('tm006')">
				<u-switch v-model="isShowSwitch" disabled></u-switch>
			</u-form-item>

			<u-form-item label="备注" v-if="judgeShow('tm007')">
				<u-input v-model="info.tm007" type="textarea" placeholder="请输入备注" disabled></u-input>
			</u-form-item>

			<u-form-item label="相关附件" v-if="judgeShow('attachmentUpload')">
				<jnpf-file v-model="info.attachmentUpload" disabled />
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in lst" :key="i">
					<u-form-item label="序号">
						{{i+1}}
					</u-form-item>
					<u-form-item label="品号" v-if="judgeShow('tn004')">
						<u-input v-model="item.tn004" placeholder="品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="品名" v-if="judgeShow('tn005')">
						<u-input v-model="item.tn005" placeholder="品名" disabled></u-input>
					</u-form-item>
					<u-form-item label="规格" v-if="judgeShow('tn006')">
						<u-input v-model="item.tn006" placeholder="规格" disabled></u-input>
					</u-form-item>
					<u-form-item label="工艺" v-if="judgeShow('tn007')">
						<u-input v-model="item.tn007" placeholder="工艺" disabled></u-input>
					</u-form-item>
					<u-form-item label="工艺名称" v-if="judgeShow('tn007Name')">
						<u-input v-model="item.tn007Name" placeholder="工艺名称" disabled></u-input>
					</u-form-item>
					<u-form-item label="计价单位" v-if="judgeShow('tn008')">
						<u-input v-model="item.tn008" placeholder="计价单位" disabled></u-input>
					</u-form-item>
					<u-form-item label="委外单价" v-if="judgeShow('tn009')">
						<u-input v-model="item.tn009" placeholder="委外单价" disabled></u-input>
					</u-form-item>
					<u-form-item label="生效日" v-if="judgeShow('tn011')">
						<u-input v-model="item.tn011" placeholder="请输入生效日" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tn012')" label="失效日">
						<u-input v-model="item.tn012" placeholder="请输入失效日" disabled></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tn010')" label="损坏扣款">
						<u-input v-model="item.tn010" placeholder="损坏扣款" :disabled="
                      judgeWrite('moctnList') || judgeWrite('moctnList-tn010')
                    "></u-input>
					</u-form-item>
					<u-form-item v-if="judgeShow('tn013')" label="备注">
						<u-input v-model="item.tn013" type="textarea" placeholder="请输入备注" disabled></u-input>
					</u-form-item>
					<u-form-item label="原单价" v-if="judgeShow('tn023')">
						<u-input v-model="item.tn023" placeholder="原单价" :disabled="
                      judgeWrite('moctnList') || judgeWrite('moctnList-tn023')
                    "></u-input>
					</u-form-item>
					<u-form-item label="原生效日" v-if="judgeShow('tn024')">
						<u-input v-model="item.tn024" placeholder="请输入原生效日" :disabled="
                      judgeWrite('moctnList') || judgeWrite('moctnList-tn024')
                    "></u-input>
					</u-form-item>
				</view>
			</view>
		</u-form>
	</view>
</template>

<script>
	import {
		getMoctmFlowInfo
	} from '@/api/workFlow/flowEngine'
	import {
		getDataInterfaceDataInfo,
		getDataInterfaceRes,
		getDictionaryDataSelector
	} from '@/api/common'
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update
	} from '@/api/apply/order'
	export default {
		name: 'crmOrder',
		mixins: [comMixin],
		data() {
			return {
				hide: false,
				billEnCode: 'OrderNumber',
				dataForm: {

				},
				lst: [],
				info: {},
				formConf: {},
				id: '',
				buyName: '',
				buyTwoName: '',
				payTypeName: '',
				statusName: '', //签核状态
				isShowSwitch: false,
			}
		},

		methods: {
			init(data, id, flowFormContentJson) {
				this.id = id
				this.setting = data
				this.getInfo(flowFormContentJson)
			},

			getInfo(data) {
				getMoctmFlowInfo({
					tm001: data.tm001,
					tm002: data.tm002
				}).then(res => {
					if (res.code == 200) {
						this.info = res.data
						this.lst = res.data.moctnList
						this.isShowSwitch = this.info.tm006 == 'Y' ? true : false
						this.getDictionaryDataSelectorFn()
						this.getDataInterfaceDataInfoMV002(this.info.tm001) //单别
						this.getDataInterfaceDataInfoMV003(this.info.tm004) //.委外供应商
						this.getDataInterfaceDataInfoMV004(this.info.tm005) //币种
					}
				})
			},
			getDictionaryDataSelectorFn() {
				getDictionaryDataSelector('393566623006470277').then(res => {
					if (res.code == 200) {
						res.data.list.forEach(res => {
							if (res.enCode == this.info.tm012) {
								this.statusName = res.fullName
							}
						})
					}
				})
			},
			getDataInterfaceDataInfoMV002(id) {
				let data = {
					id,
					interfaceId: '456337653411882373',
					dblink: this.id,
					propsValue: 'MQ001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('456337653411882373', data).then(res => {
					if (res.code == 200) {
						this.buyName = res.data.joinStr
					}
				})
			},
			getDataInterfaceDataInfoMV003(id) {
				let data = {
					id,
					interfaceId: '381247065990113925',
					dblink: this.id,
					propsValue: 'MA001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('381247065990113925', data).then(res => {
					if (res.code == 200) {
						this.buyTwoName = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDataInfoMV004(id) {
				let data = {
					id,
					interfaceId: '362115609221143109',
					dblink: this.id,
					propsValue: 'MF001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('362115609221143109', data).then(res => {
					if (res.code == 200) {
						this.payTypeName = res.data.JoinStr
					}
				})
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>